-- Pr1
import Data.List
import Control.Monad

chessBoard :: Int -> [(Int, Int)]
chessBoard n = [(a,b) | a <- [1..n], b <- [1..n]]

-- available places, current path reversed
findPath :: Int -> [(Int, Int)] -> [(Int, Int)] -> [(Int, Int)]
findPath n [] path = if length path == n*n then path else []
findPath n (x@(i,j):xs) path = let
    currentPath = findPath n availablePositions (x:path)
    followingPath = findPath n xs path
    availablePositions = filter (\el@(i, j) -> i>=1 && j>=1 && i<=n && j<=n && not (el `elem` path)) [(i+2, j+1), (i+2, j-1), (i+1, j+2), (i+1, j-2),
                            (i-2, j+1), (i-2, j-1), (i-1, j+2), (i-1, j-2) ]
    in if null currentPath then followingPath else currentPath

main = do
    let n = 5
    print $ findPath n (chessBoard n) []
